package com.lorenzogao.customview

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.lorenzogao.customview.fragment.ItemFragment
import com.lorenzogao.customviewexample.R
import com.lorenzogao.lib_view.ColorTrackTextView
import kotlinx.android.synthetic.main.activity_view_pager.*
import kotlinx.android.synthetic.main.layout_head.*
import java.lang.Exception


class ViewPagerActivity : BaseActivity() {


    private val items = arrayOf("直播", "推荐", "视频", "段子", "精华")

    private var mIndicators = mutableListOf<ColorTrackTextView>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager)

        setToolBar(toolbar, "ViewPager玩转字体变色")

        initIndiscators()
        initViewPager()

    }

    private fun initViewPager() {


        val fragments = mutableListOf<Fragment>()
        items.forEach {
            fragments.add(ItemFragment.newInstance(it))
        }

        viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager, 0) {
            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }

            override fun getCount(): Int {
                return fragments.size
            }
        }



        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                try {
                    val left=mIndicators[position]
                    left.setDirection(ColorTrackTextView.DIRECTION.RIGHT_TO_LEFT)
                    left.setCurrentProgress(1-positionOffset)
                    val right=mIndicators[position+1]
                    right.setDirection(ColorTrackTextView.DIRECTION.LEFT_TO_RIGHT)
                    right.setCurrentProgress(positionOffset)
                }catch (e:Exception){
                    e.printStackTrace()
                }

            }

            override fun onPageSelected(position: Int) {

            }

        })


    }

    private fun initIndiscators() {
        items.forEach {
            val params =
                LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            params.weight = 1f
            var colorTrackTextView = ColorTrackTextView(this)
            colorTrackTextView.setChanageColor(Color.RED)
            colorTrackTextView.text = it
            colorTrackTextView.layoutParams = params
            indicaterView.addView(colorTrackTextView)
            mIndicators.add(colorTrackTextView)
        }


    }
}
